package sg.edu.nus.iss.billing.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.AbstractCellEditor;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.text.Document;

import sg.edu.nus.iss.billing.controller.UserController;
import sg.edu.nus.iss.billing.exception.ControllerException;
import sg.edu.nus.iss.billing.exception.ValidationException;
import sg.edu.nus.iss.billing.gui.component.ErrorDialog;
import sg.edu.nus.iss.billing.gui.component.ValidateJTextField;
import sg.edu.nus.iss.billing.localization.MessageKeyManager;
import sg.edu.nus.iss.billing.privilege.AdminRole;
import sg.edu.nus.iss.billing.privilege.AgentRole;
import sg.edu.nus.iss.billing.privilege.Role;
import sg.edu.nus.iss.billing.privilege.User;
import sg.edu.nus.iss.billing.validation.ValidationRule;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class UpdateUserPanel extends DefaultFunctionPanel  {
	
	private static final long serialVersionUID = 1L;
	private JLabel lblUserinfo;
	private JLabel lblUserdetails;
	private ValidateJTextField usernameSearchTxt;
	private JButton searchButton;
	
	private UserController userController;
	
	public UpdateUserPanel(){
	    userController = new UserController();
		init("");
	}
	
	public JPanel createTopPanel() {
		JPanel searchPanel = new JPanel();
		searchPanel.setLayout(null);
	 	
	 	lblUserinfo = new JLabel("User Search Panel:");
	 	Font f = new Font("Arial",Font.BOLD, 12);
	 	lblUserinfo.setFont(f);
	 	lblUserinfo.setBounds(10, 15, 200, 30);
	 	searchPanel.add(lblUserinfo);
	 	
	 	lblUserdetails = new JLabel(MessageKeyManager.getMessage("label.username"));
	 	lblUserdetails.setBounds(10, 45, 200, 20);
	 	searchPanel.add(lblUserdetails);
	 	
	 	usernameSearchTxt = new ValidateJTextField(MessageKeyManager.getMessage("label.username"), ValidationRule.NOT_NULL);
	 	usernameSearchTxt.setBounds(150, 45, 100, 20);

        DocumentListener documentListener = new DocumentListener() {
            public void changedUpdate(DocumentEvent documentEvent) {
                updateTableList(documentEvent);
            }

            public void insertUpdate(DocumentEvent documentEvent) {
                updateTableList(documentEvent);
            }

            public void removeUpdate(DocumentEvent documentEvent) {
                updateTableList(documentEvent);
            }

            private void updateTableList(DocumentEvent documentEvent) {
                if (usernameSearchTxt.getText() != null && usernameSearchTxt.getText().length() == 0) {
                    ((UserTableListingPanel)getBottomPanel()).refreshTableWithAllUsers();
                }
            }
        };
        usernameSearchTxt.getDocument().addDocumentListener(documentListener);
	 	
	 	searchPanel.add(usernameSearchTxt);
	 	
	 	searchButton = new JButton(MessageKeyManager.getMessage("button.search"));
	 	searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    List<User> userList = new ArrayList<User>();
                    String username = usernameSearchTxt.getValidatedText();
                    User userObj = userController.getUserByName(username);
                    if (userObj != null) {
                        userList.add(userObj);
                    }
                    ((UserTableListingPanel)getBottomPanel()).refreshTable(userList);
                } catch (ValidationException ex) {
                    ErrorDialog.showValidationErrorMessageDialog(ex);
                } catch (ControllerException ex) {
                    ErrorDialog.showControllerErrorMessageDialog(ex);
                }
            }
        });
	 	
	 	searchButton.setBounds(300, 45, 100, 20);
	 	searchPanel.add(searchButton);
	 	
	 	return searchPanel;
	}
	
	public JPanel createActionButtonsPanel() {
		JPanel buttonPanel = new JPanel();
		FlowLayout thisLayout = new FlowLayout();
		thisLayout.setAlignment(FlowLayout.CENTER);
		buttonPanel.setLayout(thisLayout);
		JButton deleteButton = new JButton(MessageKeyManager.getMessage("button.delete"));
		deleteButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				List<String> selUsername = ((UserTableListingPanel)getBottomPanel()).getSelectedUsernames();
				try {
					userController.removeUserList(selUsername);
                } catch (ControllerException ex) {
                    ErrorDialog.showControllerErrorMessageDialog(ex);
                } 
                ((UserTableListingPanel)getBottomPanel()).refreshTableWithAllUsers();
			}
		});
		buttonPanel.add(deleteButton);
		return buttonPanel;
	}
	
	public JPanel createBottomPanel() {
		return new UserTableListingPanel(this);
	}
	
	

}
